/*
 *     Prionace - A file navigator to manage your files
 *
 *     Copyright (C) 2012 Francivan Bezerra
 *
 *     This file is part of Prionace.
 *
 *     Prionace is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Prionace is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Prionace.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fba.prionace.ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import org.fba.prionace.config.ShortcutStorage;
import org.fba.prionace.file.JFile;

public class ShortcutsPane extends JScrollPane implements ComponentListener {

	private ButtonGroup group;

	private Vector<JFile> shortcuts;

	private JPanel buttonsPane;

	public ShortcutsPane() {
		super();
		init();
	}

	private void init() {
		group = new ButtonGroup();
		shortcuts = new Vector<JFile>();
		buttonsPane = new JPanel();

		JLabel title = new JLabel("Atalhos");
		title.setFont(title.getFont().deriveFont(Font.BOLD));

		JFile home = new JFile(System.getProperty("user.home"));
		JFile root = new JFile("/");

		String osName = System.getProperty("os.name").toLowerCase();
		root.setText(osName.contains("win") ? "Computador"
				: "Sistema de arquivos");
		home.setText("Home");

		home.setToolTipText(null);
		root.setToolTipText(null);

		home.setIconSize(new Dimension(16, 16));
		root.setIconSize(new Dimension(16, 16));

		home.setHorizontalTextPosition(SwingConstants.RIGHT);
		root.setHorizontalTextPosition(SwingConstants.RIGHT);

		home.setHorizontalAlignment(SwingConstants.LEADING);
		root.setHorizontalAlignment(SwingConstants.LEADING);

		root.setActionCommand("/");

		group.add(home);
		group.add(root);

		buttonsPane.setLayout(new BoxLayout(buttonsPane, BoxLayout.Y_AXIS));
		buttonsPane.add(title);
		buttonsPane.add(createSeparator());
		buttonsPane.add(home);
		buttonsPane.add(root);
		buttonsPane.add(createSeparator());

		setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		setTransferHandler(new ShortcutTransferHandler());
		getVerticalScrollBar().setUnitIncrement(14);
		setViewportView(buttonsPane);
		addComponentListener(this);

		restoreShortcuts();
	}

	public void restoreShortcuts() {
		try {
			Vector<String> shortcuts = ShortcutStorage.restore();

			for (String shortcut : shortcuts) {
				addShortcut(new File(shortcut));
			}
		} catch (IOException e) {
			ExceptionHandler.showException(e, this);
		}
	}

	public JPanel getButtonsPane() {
		return buttonsPane;
	}

	public Vector<JFile> getShortcuts() {
		return shortcuts;
	}

	public void addShortcut(File path) {
		if (!path.isDirectory()) {
			return;
		}

		JFile shortcut = new JFile(path);
		shortcut.setToolTipText(null);
		shortcut.setIconSize(new Dimension(16, 16));
		shortcut.setHorizontalTextPosition(SwingConstants.RIGHT);
		shortcut.setHorizontalAlignment(SwingConstants.LEADING);
		shortcut.setMaximumSize(new Dimension(getViewport().getSize().width,
				shortcut.getPreferredSize().height));
		shortcut.setSize(shortcut.getMaximumSize());

		shortcuts.add(shortcut);
		group.add(shortcut);
		buttonsPane.add(shortcut);

		TrayControl tray = TrayControl.getInstance();
		TrayMenuItem menuItem = new TrayMenuItem(shortcut.getText(), shortcut
				.getFile().getAbsolutePath());
		menuItem.addActionListener(tray);
		tray.getMenu().add(menuItem);

		revalidate();
	}

	public void selectShortcutFor(File path) {
		group.clearSelection();
		Enumeration<AbstractButton> buttons = group.getElements();
		JFile shortcut;

		do {
			shortcut = (JFile) buttons.nextElement();
		} while (buttons.hasMoreElements() && !shortcut.getFile().equals(path));

		if (shortcut.getFile().equals(path)) {
			shortcut.setSelected(true);
		}
		repaint();
	}

	private JSeparator createSeparator() {
		JSeparator separator = new JSeparator();
		Dimension size = new Dimension(separator.getMaximumSize().width,
				separator.getPreferredSize().height);
		separator.setMaximumSize(size);
		return separator;
	}

	public void componentResized(ComponentEvent e) {
		Enumeration<AbstractButton> buttons = group.getElements();
		AbstractButton button;

		while (buttons.hasMoreElements()) {
			button = buttons.nextElement();
			button.setMaximumSize(new Dimension(getViewport().getSize().width,
					button.getPreferredSize().height));
			button.setSize(button.getMaximumSize());
		}
	}

	public void componentMoved(ComponentEvent e) {
	}

	public void componentShown(ComponentEvent e) {
	}

	public void componentHidden(ComponentEvent e) {
	}
}
